<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['sms:flashPromotionProductRelation:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['sms:flashPromotionProductRelation:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['sms:flashPromotionProductRelation:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['sms:flashPromotionProductRelation:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="flashPromotionProductRelationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="商品名称" align="center" prop="name" />
      <el-table-column label="商品价格">
        <template slot-scope="scope">
          {{ '￥' + scope.row.price }}
        </template>
      </el-table-column>
      <el-table-column label="限时购价格" align="center">
        <template slot-scope="scope">
          {{ scope.row.flashPromotionPrice ? '￥' + scope.row.flashPromotionPrice : '' }}
        </template>
      </el-table-column>
      <el-table-column label="活动商品数量" align="center" prop="flashPromotionCount" />
      <el-table-column label="限购数量" align="center" prop="flashPromotionLimit" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['sms:flashPromotionProductRelation:edit']"
          >修改</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['sms:flashPromotionProductRelation:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加商品限时购与商品关系对话框 -->
    <el-dialog title="选择商品" :visible.sync="open" width="500px" append-to-body>
      <el-table v-loading="productLoading" :data="productList" @selection-change="handleProductSelectionChange" border row-key="id">
        <el-table-column type="selection" width="55" align="center" :reserve-selection="true"/>
        <el-table-column label="编号" align="center" prop="id" />
        <el-table-column label="商品名称" align="center" prop="name" />
        <el-table-column label="价格" align="center" prop="price">
          <template slot-scope="scope">
            {{ '￥' + scope.row.price }}
          </template>
        </el-table-column>
      </el-table>
      <pagination
          v-show="productTotal>0"
          :total="productTotal"
          :page.sync="productQueryParams.pageNum"
          :limit.sync="productQueryParams.pageSize"
          layout="total, sizes, prev, pager, next"
          @pagination="getList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAddForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑限时购商品信息" :visible.sync="editOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="商品名称">
          <span>{{ form.name }}</span>
        </el-form-item>
        <el-form-item label="商品价格">
          <span>{{ '￥' + form.price }}</span>
        </el-form-item>
        <el-form-item label="限时购价格" prop="flashPromotionPrice">
          <el-input v-model="form.flashPromotionPrice">
            <template v-slot:prepend>￥</template>
          </el-input>
        </el-form-item>
        <el-form-item label="活动商品数量" prop="flashPromotionCount">
          <el-input type="number" :min="0" v-model="form.flashPromotionCount" />
        </el-form-item>
        <el-form-item label="限购数量" prop="flashPromotionLimit">
          <el-input type="number" :min="0" v-model="form.flashPromotionLimit" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input type="number" :min="0" v-model="form.sort" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitEditForm">确 定</el-button>
        <el-button @click="editOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFlashPromotionProductRelation, getFlashPromotionProductRelation, delFlashPromotionProductRelation, addFlashPromotionProductRelation, updateFlashPromotionProductRelation, exportFlashPromotionProductRelation } from "@/api/sms/flashPromotionProductRelation";
import { listProduct } from "@/api/pms/product";

export default {
  name: "FlashPromotionProductRelation",
  components: {
  },
  computed: {
    flashPromotionSessionId() {
      return this.$route.query.flashPromotionSessionId;
    },
    flashPromotionId() {
      return this.$route.query.flashPromotionId;
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品限时购与商品关系表格数据
      flashPromotionProductRelationList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      productQueryParams: {
        pageNum: 1,
        pageSize: 10
      },
      productTotal: 0,
      productLoading: true,
      productList: [],
      productIds: [],
      form: {},
      rules: {
        flashPromotionPrice: [
          { required: true, message: "限时购价格不能为空", trigger: "blur" }
        ],
        flashPromotionCount: [
          { required: true, message: "活动商品数量不能为空", trigger: "blur" }
        ],
        flashPromotionLimit: [
          { required: true, message: "限购数量不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
      },
      editOpen: false
    };
  },
  created() {
    this.getList();
    this.getProductList();
  },
  methods: {
    /** 查询商品限时购与商品关系列表 */
    getList() {
      this.loading = true;
      this.queryParams.flashPromotionId = this.flashPromotionId;
      this.queryParams.flashPromotionSessionId = this.flashPromotionSessionId;
      listFlashPromotionProductRelation(this.queryParams).then(response => {
        this.flashPromotionProductRelationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getProductList() {
      this.productLoading = true;
      listProduct(this.productQueryParams).then(response => {
        this.productList = response.rows;
        this.productTotal = response.total;
        this.productLoading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "选择商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form = {};
      this.resetForm('form');
      const id = row.id || this.ids
      getFlashPromotionProductRelation(id).then(response => {
        this.form = response.data;
        this.editOpen = true;
      });
    },
    /** 提交按钮 */
    submitAddForm() {
      const data = this.productIds.map(id => {
        return {
          flashPromotionId: this.flashPromotionId,
          flashPromotionSessionId: this.flashPromotionSessionId,
          productId: id
        };
      });
      addFlashPromotionProductRelation(data).then(response => {
        this.msgSuccess('添加成功');
        this.open = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除商品限时购与商品关系编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delFlashPromotionProductRelation(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有商品限时购与商品关系数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportFlashPromotionProductRelation(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    },
    handleProductSelectionChange(selection) {
      this.productIds = selection.map(item => item.id);
    },
    submitEditForm() {
      updateFlashPromotionProductRelation(this.form).then(response => {
        this.msgSuccess('修改成功');
        this.getList();
        this.editOpen = false;
      });
    }
  }
};
</script>